package dot;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.jgrapht.Graph;
import org.jgrapht.ext.VertexNameProvider;
import org.jgrapht.graph.DefaultEdge;

import cover.vertex.Vertex;

public class Export {
	/**
	 * Exports a graph into a plain text file in DOT format.
	 * @param fileName
	 * @param graph
	 */
	public Export() {
	}

	public void exportToDot(String path, Graph<Vertex, DefaultEdge> graph) {
		DOTExporter<Vertex, DefaultEdge> exporter = new DOTExporter<Vertex, DefaultEdge>(
				new VertNameProvider(),
				new VertAtributeProvider(), null);

		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(path, false));
			exporter.export(out, graph);
			out.close();
		} catch (IOException e) {
			System.out.println("exception on writing to file");
			e.printStackTrace();
		}
	}

	private class VertNameProvider implements VertexNameProvider<Vertex> {

		public String getVertexName(Vertex vertex) {
			Vertex vertex2 = (Vertex) vertex;
			return vertex2.getName();
		}
	}

	private class VertAtributeProvider implements VertexNameProvider<Vertex> {

		public String getVertexName(Vertex vertex) {
			Vertex vertex2 = (Vertex) vertex;
			return "[label=\"" + vertex2.getName() + ", "
					+ vertex2.getCover() + "\"]";
		}
	}

}
